package pers.qianyu.month_202102.date_20210207.weekly_contest_227;


import org.junit.*;

/**
 * [第 227 场周赛 第三题]
 * 5674. 构造字典序最大的合并字符串
 * https://leetcode-cn.com/problems/largest-merge-of-two-strings/
 *
 * @author mizzle rain
 * @date 2021年2月7日11:42:05
 */
public class LargestMerge {
    public String largestMerge(String word1, String word2) {
        int m = word1.length(), n = word2.length();
        int p1 = 0, p2 = 0;
        StringBuilder builder = new StringBuilder();
        while (p1 < m && p2 < n) {
            String sub1 = word1.substring(p1), sub2 = word2.substring(p2);
            if (sub1.compareTo(sub2) > 0) {
                builder.append(word1.charAt(p1++));
            } else {
                builder.append(word2.charAt(p2++));
            }
        }
        if (p1 < m) builder.append(word1, p1, word1.length());
        if (p2 < n) builder.append(word2, p2, word2.length());
        return builder.toString();
    }

    @Test
    public void test1() {
        String result = new LargestMerge().largestMerge("cabaa", "bcaaa");
        System.out.println("result = " + result);
        Assert.assertEquals(result, "cbcabaaaaa");
    }

    @Test
    public void test2() {
        String result = new LargestMerge().largestMerge("abcabc", "abdcaba");
        System.out.println("result = " + result);
        Assert.assertEquals(result, "abdcabcabcaba");
    }
}